#include <stdio.h>
#include <stdlib.h>

#define LH 1
#define EH 0
#define RH -1

#define TRUE 1
#define FALSE 0

// 平衡二叉树 AVL树
typedef struct balancedBinaryTree
{
    /* data */
    // 数据
    int data;
    // 平衡因子
    int bf;
    // 树高
    int height;
    Tree *left, *right;
} Tree;

Tree *CreateTreeNode(int i)
{
    Tree *node = (Tree *)malloc(sizeof(Tree));
    if (NULL == node)
    {
        printf("node malloc error！");
        exit(-1);
    }
    node->data = i;
    node->bf = EH;
    node->height = 0;
    node->left = NULL;
    node->right = NULL;
    return node;
}

void leftUnbalance(Tree *T)
{
    Tree *children = T->left;
    switch (children->bf)
    {
    case LH:
        T->bf = children->bf = EH;
        break;
    case RH:
        Tree *grandson = children->right;
        switch (grandson->bf)
        {
        case LH:
            /* code */
            grandson->bf = children->bf = EH;
            T->bf = RH;
            break;
        case RH:
            /* code */
            grandson->bf = T->bf = EH;
            children->bf = LH;
            break;
        case EH:
            T->bf = children->bf = EH;
            break;
        default:
            break;
        }
        break;
    default:
        break;
    };
}

void rightUnbalance(Tree *T)
{
    Tree *children = T->right;
    switch (children->bf)
    {
    case LH:
        Tree *grandson = children->left;
        switch (grandson->bf)
        {
        case LH:
            /* code */
            grandson->bf = T->bf = EH;
            children->bf = RH;
            break;
        case RH:
            /* code */
            grandson->bf = children->bf = EH;
            T->bf = LH;
            break;
        case EH:
            T->bf = children->bf = EH;
            break;
        default:
            break;
        }
        break;
    case RH:
        T->bf = children->bf = EH;
        break;
    default:

        break;
    };
}

void leftRotation(Tree *node)
{

}

void rightRotation(Tree *node)
{
    
}